﻿// Call this function to dynamically load the calendar with events from the database for a date range.
// this will save loading time as current view (day, week, month) defines the date range.
function GetEventsInDateRange(start, end, roomCode) {
    var tempArray = null;
    var webServiceAddress = $('#MainContent_serverPathHiddenField').val() + "schedulerWebService.asmx/";
    var eventArray = new Array();
    var roomCode = $('#MainContent_roomDropDownList option:selected').val();

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: webServiceAddress + "GetEventsForRoomInRange",
        data: "{ startDateRange: '" + NormalizeDateToString(start) + "', endDateRange: '" + NormalizeDateToString(end) + "', roomCode: '" + roomCode + "' }",
        dataType: "xml",
        async: false,
        success: function (data, textStatus, XMLHttpRequest) {
            // Convert XML to JavaScript object
            $(data).find("CalendarEvent").each(function () {
                var event = new Object();

                event.id = $(this).find("Id").text();
                event.title = $(this).find("Title").text();
                event.allDay = ($(this).find("AllDay").text() == "true") ? false : false;
                event.start = $(this).find("Start").text();
                event.end = $(this).find("End").text();
                event.url = $(this).find("Url").text(); // Not needed.
                event.className = $(this).find("ClassName").text();
                event.editable = ($(this).find("Editable").text() == "true") ? true : false;
                event.roomCode = $(this).find("RoomCode").text();

                tempArray.push(event);
            });

            return tempArray;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest + " Failure: " + textStatus + "->" + errorThrown);
        }
    });

    return null;
}

// Save a single event to database
function SaveEventToDB(calEvent) {
    if (calEvent.editable) {

        var webServiceAddress = $('#MainContent_serverPathHiddenField').val() + "schedulerWebService.asmx/";
        var roomCode = $('#MainContent_roomDropDownList option:selected').val();

        var datastring = "{";
        
        datastring += " Id: '" + calEvent.id.toString() + "',";
        datastring += " Title: '" + calEvent.title.toString() + "',";
        datastring += " AllDay: '" + ((calEvent.allDay) ? calEvent.allDay.toString() : "False") + "',";

        var start = ((calEvent.start) ? NormalizeDateToString(calEvent.start).toString() : "NULL");
        datastring += " Start: '" + start + "',";
        datastring += " End: '" + ((calEvent.end) ? NormalizeDateToString(calEvent.end).toString() : start) + "',";

        datastring += " Url: '" + ((calEvent.url) ? calEvent.url.toString() : "NULL") + "',";
        datastring += " ClassName: '" + ((calEvent.className) ? calEvent.className.toString() : "NULL") + "',";
        datastring += " Editable: '" + ((calEvent.editable) ? calEvent.editable.toString() : "True") + "',";
        datastring += " RoomCode: '" + roomCode.toString() + "'";
        
        datastring += "}";
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: webServiceAddress + "SaveEventString",
            data: datastring,
            async: false,
            dataType: "json",
            success: function (data, textStatus, XMLHttpRequest) {
                var myResult = data.d;
                //prompt(myResult + " String: " , datastring);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest + " Failure: " + textStatus + "->" + errorThrown + " String: " + datastring);
                alert('Start Save');
            }
        });
    }
}

// Accepts Date object and returns string "xx/xx/xxxx"
function NormalizeDateToString(dateObj) {
    if (dateObj) {
        try {
            //dateObj = new Date(dateObj);
            var dateString = dateObj.format("yyyy/M/dd HH:mm:ss");
            var dateNull = 'NaN/NaN/NaN';
            if (dateString == dateNull) {
                dateString = '1/1/1753';
            }
        } catch (e) {
            dateString = '1/1/1753';
        }
        finally {
            return dateString;
        }
    }
    return dateObj;
}